# Maintainer: J. Peter Mugaas <jpmugaas@suddenlink.net>

pkgbase='libpsl'
pkgname=("${pkgbase}" "${pkgbase}-devel")
pkgver=0.20.2
pkgrel=1
pkgdesc='Public Suffix List library'
url='https://github.com/rockdaboot/libpsl'
arch=('i686' 'x86_64')
license=('MIT')
makedepends=('libxslt' 'python3' 'libidn2-devel' 'publicsuffix-list' 'libunistring-devel')
source=(https://github.com/rockdaboot/libpsl/releases/download/${pkgname}-${pkgver}/${pkgname}-${pkgver}.tar.gz
        0.16.0-install-dafsa.patch 
        0.16.1-make-dafsa-setlocale.patch
        019.1-implement-private-libs.patch)
sha512sums=('fa9f6f7f0447d9fe00f5dfca5262c56ff26217eea44d0f7fc1e5d982224c41874e753f0aa06dd9e5d7d03d4f04e3dacd4f36034cc8dd0fc6e2c28b49a23e62fe'
            'c8c7c38c511815738aae293a72f7527926a27a7df28df47aa812deb472cc95ff620403fc9f120b61b10d1bc5a84cc5608fda4acc49337cdd07861ecf1ca76c13'
            '2218345c6f70b0ff9e052b92e80cf23bf86e02802abab8e640f2250bfceee104f5ea1603fa1cc7dd135c75b9acfe8f10541d0689f4b2d313fb07ef6140969604'
            'e38456f0b5b5cd72e64abcc0aa8bfc4abef43925f33d77a8e4bd1e83ac7f40b9180fe72b77a19b5485433db5155cd8ab8c1a479f0f4286032f62fe6bdd5d8461')

# Helper macros to help make tasks easier #
apply_patch_with_msg() {
  for _patch in "$@"
  do
    msg2 "Applying $_patch"
    patch -Nbp1 -i "${srcdir}/$_patch"
  done
}

apply_patch_p2_with_msg() {
  for _patch in "$@"
  do
    msg2 "Applying $_patch"
    patch -Nbp2 -i "${srcdir}/$_patch"
  done
}

del_file_exists() {
  for _fname in "$@"
  do
    if [ -f $_fname ] || [ -d $_fname ]; then
      rm -rf $_fname
    fi
  done
}
# =========================================== #

prepare() {
  cd ${pkgbase}-${pkgver}
  apply_patch_p2_with_msg \
    0.16.0-install-dafsa.patch \
    0.16.1-make-dafsa-setlocale.patch \
    019.1-implement-private-libs.patch

  rm -frv list
  autoreconf -fiv
}

build() {
  cd ${pkgbase}-${pkgver}
  
  [[ -d "${srcdir}"/build-${CARCH} ]] && rm -rf "${srcdir}"/build-${CARCH}
  mkdir -p "${srcdir}"/build-${CARCH} && cd "${srcdir}"/build-${CARCH}
  PYTHON=/usr/bin/python3.exe ../${pkgname}-${pkgver}/configure \
    --prefix=/usr \
    --disable-dependency-tracking \
    --enable-shared \
    --enable-static \
    --enable-man \
    --enable-builtin=libidn2 \
    --enable-runtime=libidn2 \
    --with-psl-file=/usr/share/publicsuffix/public_suffix_list.dat \
    --with-psl-testfile=/usr/share/publicsuffix/test_psl.txt
  LC_CTYPE=en_US.UTF-8 make
  make DESTDIR="${srcdir}/dest" install
}

check() {
  cd "${srcdir}"/build-${CARCH}
  make check
}

package_libpsl() {
 pkgdesc="Public Suffix List library (runtime)"
  depends=('libxslt' 'libidn2' 'libunistring')
  groups=('libraries')

  mkdir -p ${pkgdir}/usr/bin
  cp -f ${srcdir}/dest/usr/bin/* ${pkgdir}/usr/bin/
  mkdir -p ${pkgdir}/usr/share/man
  cp -rf ${srcdir}/dest/usr/share/man/man1 ${pkgdir}/usr/share/man/
  install -Dm 644 ${srcdir}/${pkgbase}-${pkgver}/COPYING -t "${pkgdir}/usr/share/licenses/${pkgname}"

}

package_libpsl-devel() {
  pkgdesc="libpsl headers and libraries"
  options=('staticlibs')
  depends=("libpsl=${pkgver}" 'libxslt' 'libidn2-devel' 'libunistring')
  groups=('development')

  mkdir -p ${pkgdir}/usr/share/man/man3
  cp -rf ${srcdir}/dest/usr/include ${pkgdir}/usr/
  cp -rf ${srcdir}/dest/usr/lib ${pkgdir}/usr/

}

# vim: ts=2 sw=2 et:
